const fs = require('fs').promises;
const path = require('path');
const { config } = require('./config');

const Config = config()

const getNoteGrope = () => { // 获取笔记组
    return new Promise((resolve, reject) => {
        fs.readFile(Config.note_path + "\\note\\config.json", "utf-8").then(data => {
            let note = JSON.parse(data);
            resolve([note])
        }).catch(err => {
            reject(err)
        })
    })
}

module.exports = {
    creatNoteGroup: (name) => { // 创建笔记组
        return new Promise((resolve, reject) => {
            fs.readFile(Config.note_path + "\\note\\config.json", "utf-8").then(data => {
                let note = JSON.parse(data);
                let newData = {
                    name: name,
                    notes: []
                }
                note.push(newData)
                fs.writeFile(Config.note_path + "\\note\\config.json", JSON.stringify(note), "utf-8").then(() => {
                    resolve([newData, note])
                }).catch(err => {
                    reject(err)
                })
            }).catch(err => {
                reject(err)
            })
        })
    },
    getNoteGroup: getNoteGrope,
    creatNote: (name, grope) => { // 创建笔记
        return new Promise((resolve, reject) => {
            fs.readFile(Config.note_path + "\\note\\config.json", "utf-8").then(data => {
                let note = JSON.parse(data);
                let pass = false;
                for (let i = 0; i < note.length; i++) {
                    if (pass) {
                        reject("软件错误， 请联系开发者")
                    }
                    let ele = note[i];
                    if (ele.name == grope) {
                        pass = !pass
                        let date = new Date()
                        let id = DMath.floor(date.getTime() / 1000)
                        let newData = {
                            name: name,
                            path: Config.note_path + "\\note\\" + id + ".mj",
                            id: id,
                            time: id,
                            type: "deafult"
                        }
                        note[i].notes.push(newData)
                        // 创建文件
                        fs.writeFile(Config.note_path + "\\note\\" + id + ".mj", "", "utf-8").then(() => {
                            fs.writeFile(Config.note_path + "\\note\\config.json", JSON.stringify(note), "utf-8").then(() => {
                                resolve([newData, note])
                            }).catch(err => {
                                reject(err)
                            })
                        }).catch(err => {
                            reject(err)
                        })
                    }
                }
            })
        })
    },
    deleteNote: (id) => { // 删除笔记
        return new Promise((resolve, reject) => {
            // 读取配置文件
            getNoteGrope().then(res => {
                for (let i = 0; i < res[0].length; i++) { // 第一层循环
                    let ele = res[0][i]
                    for (let j = 0; j < ele.notes.length; j++) { // 循环第二层，寻找笔记
                        let note = ele.notes[j]
                        if (note.id == id) {
                            let delEle = ele.notes[j]
                            ele.notes.splice(j, 1)
                            fs.writeFile(Config.note_path + "\\note\\config.json", JSON.stringify(res), "utf-8").then(() => {
                                // 删除文件
                                fs.unlink(Config.note_path + "\\note\\" + id + ".mj").then(() => {
                                    resolve([delEle, res])
                                }).catch(err => {
                                    reject(err)
                                })

                            }).catch(err => {
                                reject(err)
                            })
                        }
                    }
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}



//[
//    {
//        name: "note",
//        notes: [
//            {
//                name: "",
//                path: "",
//                id: unix,
//                time: unix,
//                type: ""
//            }
//        ]
//    }
//]
